clear all; addpath('auxiliar'); addpath('Simulated_Datasets'); load SP_SIMULATION_20k.mat
load Taxes_NoGov % FROM NO GOV EXTENSION
%% TAXES AT THE ERGODIC
TAUSIM(TAUSIM<0)=0;
%TAUSIM(TAUSIM>1)=1;
Tau_SIM_NOGOV(Tau_SIM_NOGOV<0)=0;




%% Optimal Policy in Equilibruim
mY=mean(priceSIM.*yNSIM+ySIM);
mean_ind=1;
bg=10;
shoc=13;
EBGp=sum(BG_bignext.*G_big,4);
Eq_bp=sum(G_big.*bp,4);
Eq_bmax=sum(G_big.*bpmax,4);
JTau1p=sum(G_big.*Tau1p,4);
bgrid_plot=bgrid1d+BGgrid(bg)*cte_debtfactor;

%FOR THE KINK
kink=20;
bgrid_plot1=zeros(NB+1,1);
EBGp1=zeros(NB+1,1);
Eq_bp1=zeros(NB+1,1);
Eq_bmax1=zeros(NB+1,1);
JTau1p1=zeros(NB+1,1);

bgrid_plot1(1:kink)=bgrid_plot(1:kink);
EBGp1(1:kink)=EBGp(bg,1:kink,shoc);
Eq_bp1(1:kink)=Eq_bp(bg,1:kink,shoc);
Eq_bmax1(1:kink)=Eq_bmax(bg,1:kink,shoc);
JTau1p1(1:kink)=JTau1p(bg,1:kink,shoc);

JTau1p1(kink+1)=NaN;
bgrid_plot1(kink+1)=bgrid_plot(kink)+0.0001;
EBGp1(kink+1)=EBGp(bg,kink,shoc);
Eq_bp1(kink+1)=Eq_bp(bg,kink,shoc);
Eq_bmax1(kink+1)=Eq_bmax(bg,kink,shoc);

bgrid_plot1(kink+2:end)=bgrid_plot(kink+1:end);
EBGp1(kink+2:end)=EBGp(bg,kink+1:end,shoc);
JTau1p1(kink+2:end)=JTau1p(bg,kink+1:end,shoc);
Eq_bmax1(kink+2:end)=Eq_bmax(bg,kink+1:end,shoc);
Eq_bp1(kink+2:end)=Eq_bp(bg,kink+1:end,shoc);



%% OPTIMAL POLICY IF NO PUBLIC DEBT
mY=mean(YSIM_SP_NOGOV);

shoc=13;

%FOR THE KINK
kink=18;
bgrid_NOGOV1=zeros(NB+1,1);
EBGp1=zeros(NB+1,1);
bp2D1=zeros(NB+1,1);
bpmax2D1=zeros(NB+1,1);
tau_NoGov1=zeros(NB+1,1);

bgrid_NOGOV1(1:kink)=bgrid_NOGOV(1:kink);
bp2D1(1:kink)=bp2D(1:kink,shoc);
bpmax2D1(1:kink)=bpmax2D(1:kink,shoc);
tau_NoGov1(1:kink)=tau_NoGov(1:kink,shoc);

tau_NoGov1(kink+1)=NaN;
bgrid_NOGOV1(kink+1)=bgrid_NOGOV(kink)+0.0001;
bp2D1(kink+1)=bp2D(kink,shoc);
bpmax2D1(kink+1)=bpmax2D(kink,shoc);

bgrid_NOGOV1(kink+2:end)=bgrid_NOGOV(kink+1:end);
tau_NoGov1(kink+2:end)=tau_NoGov(kink+1:end,shoc);
bpmax2D1(kink+2:end)=bpmax2D(kink+1:end,shoc);
bp2D1(kink+2:end)=bp2D(kink+1:end,shoc);


%% Density: Overlayes
 TAUSIM1=TAUSIM;
 TAUSIM1(TAUSIM1==0)=1e-200;
  Tau_SIM_NOGOV1=Tau_SIM_NOGOV;
 Tau_SIM_NOGOV1(Tau_SIM_NOGOV1==0)=1e-200;

edges = [0:0.25:15];
figure
h0=histogram(TAUSIM*100,edges,'Normalization','probability');
h0.FaceColor = 'r';
h0.EdgeColor = 'r';

hold on
h1=histogram(Tau_SIM_NOGOV*100,edges,'Normalization','probability');
h1.FaceColor = 'b';
h1.EdgeColor = 'b';
  legend('Optimal taxes','Optimal taxes with no public debt','Location','northeast');
 xlabel('Tax rate in percent','Interpreter','Latex'); ylabel('Probability ');legend boxoff;hold off
 set(gca, 'FontSize',15)
 xlim([0 15])
 ylim([0 .265])
 exportfig(gcf,'Plots\Figure11_Optimal_Tax_Density.eps','color','cmyk','resolution',1600)

